import { create, update, remove, query } from '../../../../framework/service/index';
import { ToastTips } from '../../../../common/toastTips/tosat';
import formatList from '../commonJs/formatListGroup';

export default {

  namespace: 'problemResponse',

  state: {
    problemResponseInfo: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/problem-response') {
          //获取咨询信息
          dispatch({
            type: 'getProblemResponseInfo',
          })
        }
      })
    },
  },

  effects: {
    //获取咨询信息
    *getProblemResponseInfo({ payload }, { call, put }) {
      const {code, data, message } = yield call(query, '/api/job/job-consult/me', {});
      // console.log('获取咨询信息 code', JSON.stringify(code));
      // console.log('获取咨询信息 data', JSON.stringify(data));
      // console.log('获取咨询信息 message', JSON.stringify(message));
      if (code === 200 && data) {

        // const newList = formatList.groupByList(data);

        yield put({
          type: 'querySuccess',
          payload: {
            problemResponseInfo: data
          }
        });
      }else{
        console.log('获取咨询信息失败');
      }
    },

    //修改信息为已读操作
    *putCloseMessage({ payload }, { call, put, select }) {

      let { problemResponseInfo } = yield select(({ problemResponse }) => problemResponse);

      const { id } = payload;

      const {code, data, message } = yield call(update, `/api/job/job-consult/${id}/read`, {});
      // console.log('获取消息信息 code', JSON.stringify(code));
      // console.log('获取消息信息 data', JSON.stringify(data));
      // console.log('获取消息信息 message', JSON.stringify(message));
      if (code === 200) {

        const newList = problemResponseInfo.map((item, index) => {
          if(item.id == id){
            item.readed = 1 ;
          }
          return item;
        })

        yield put({
          type: 'querySuccess',
          payload: {
            partTimeMessageInfo: newList
          }
        });
      }else{
        console.log('修改信息为已读操作失败');
      }
    }
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
